package com.sk.sourcecircle.activity.mine.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.OrderDetailsActivity;
import com.sk.sourcecircle.activity.home.activity.OtherOrderDetailsAct;
import com.sk.sourcecircle.activity.home.activity.WaitPayOrderActivity;
import com.sk.sourcecircle.adapter.OrderListAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.OrderDetailsBean;
import com.sk.sourcecircle.entity.OrderListBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sk on 2017/11/23.
 */

public class AlreadyCancelFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private OrderListAdapter adapter;
    private List<OrderListBean.DataBean> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;
    private int page = 1;
    private LoadingDialog loadingDialog;

    @Override
    protected int getContentView() {
        return R.layout.fragment_order;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new OrderListAdapter(R.layout.item_order, allDataList);

        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        dataRyc.setAdapter(adapter);

        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnItemChildClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                OrderListBean orderListBean = JSON.parseObject(result, OrderListBean.class);
                List<OrderListBean.DataBean> dataBeanList = orderListBean.getData();
                allDataList.addAll(dataBeanList);
                if (allDataList.size() < 10) {
                    adapter.setEnableLoadMore(false);
                } else {
                    adapter.setEnableLoadMore(true);
                }
                if (isLoadMore) {
                    adapter.loadMoreComplete();
                    if (dataBeanList.size() == 0) {
                        adapter.loadMoreEnd();
                    }
                    isLoadMore = false;
                }
                adapter.notifyDataSetChanged();
                break;
            case 2:
                OrderDetailsBean orderDetailsBean = JSON.parseObject(result, OrderDetailsBean.class);
                Intent intent = new Intent(mContext, WaitPayOrderActivity.class);
                intent.putExtra("data", orderDetailsBean.getData());
                startActivity(intent);
                break;
            case 4:
                OrderDetailsBean orderDetailsBean1 = JSON.parseObject(result, OrderDetailsBean.class);
                Intent intent1 = new Intent(mContext, OrderDetailsActivity.class);
                intent1.putExtra("data", orderDetailsBean1.getData());
                startActivity(intent1);
                break;
            case 6:
                OrderDetailsBean orderDetailsBean2 = JSON.parseObject(result, OrderDetailsBean.class);
                Intent intent2 = new Intent(mContext, OtherOrderDetailsAct.class);
                intent2.putExtra("data", orderDetailsBean2.getData());
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (allDataList.get(position).getOrder_status() == 1 && allDataList.get(position).getPay_status() != 2) {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 2, getActivity());
        } else if ((allDataList.get(position).getOrder_status() == 0) || (allDataList.get(position).getOrder_status() == 1 && allDataList.get(position).getPay_status() == 2)) {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 4, getActivity());
        } else {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 6, getActivity());
        }
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.myOrderList(3, page, this, 0, getActivity());
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (allDataList.get(position).getOrder_status() == 1 && allDataList.get(position).getPay_status() != 2) {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 2, getActivity());
        } else if ((allDataList.get(position).getOrder_status() == 0) || (allDataList.get(position).getOrder_status() == 1 && allDataList.get(position).getPay_status() == 2)) {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 4, getActivity());
        } else {
            loadingDialog.show();
            AppApi.getOrderDetails(allDataList.get(position).getOrder_id(), AlreadyCancelFragment.this, 6, getActivity());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        allDataList.clear();
        AppApi.myOrderList(3, page, this, 0, getActivity());
    }
}
